<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core_1_1" %>
<%--
  Created by IntelliJ IDEA.
  User: 润
  Date: 2021/7/25
  Time: 20:28
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

    <script type="text/javascript" src="jquery-1.7.2.min.js"></script>
    <script type="text/javascript">

            // var cid2=document.getElementsByClassName("insert2");
           //  var number = document.getElementsByClassName("number");
            // for(var i=0;i<number.length;i++){
              //   var cid1= cid2[number[i]-1]
               //  var i=1;
            $(function () {
                $("button[id=insert]").each(function () {
                    $(this).bind("click",function () {
                        var cid=$(this).attr("value");
                        var sid=${sessionScope.student.id};
                        var para= {

                            action:'queryAll',

                            username:${sessionScope.student.username},

                            password:${sessionScope.student.password}
                        };
                        $.ajax({
                            type:"POST",
                            url:"http://localhost:8080/Anyview/StudentServlet?action=ifHaveChoose",
                            data:{"cid":cid,"sid":sid},
                            success:function (data) {
                                if(data.result1){
                                    alert("已选不可重复！")
                                }
                                if(data.result2){
                                    alert("选课成功！");
                                    window.location.href="http://localhost:8080/Anyview/StudentServlet?"+$.param(para);
                                }
                                if(data.result3){
                                    alert("未到选课时间！");
                                }
                                if(data.result4){
                                    alert("人数已满！");
                                }
                                if(data.result5){
                                    alert("课程时间冲突！");
                                }
                            },
                            dataType:"json"
                        })


                    })

                })


                    })





$(function () {
    $("button[id=delete]").each(function () {
        $(this).bind("click",function () {
            var cid=$(this).attr("value");
            var sid=${sessionScope.student.id};
            var para= {

                action:'queryAll',

                username:${sessionScope.student.username},

                password:${sessionScope.student.password}
            };
            $.ajax({
                type:"POST",
                url:"http://localhost:8080/Anyview/StudentServlet?action=deleteCource",
                data:{"cid":cid,"sid":sid},
                success:function (data) {
                    if(data.result1){
                        alert("还未选课！")
                    }
                    if(data.result2){
                        alert("退选成功！");
                        window.location.href="http://localhost:8080/Anyview/StudentServlet?"+$.param(para);
                    }

                },
                dataType:"json"
            })


        })

    })

})








       /* $(function () {
            $(".insert").click(function () {
                var cid=document.getElementsByClassName("insert2");

                var sid=
                $.ajax({
                    type:"POST",
                    url:"http://localhost:8080/Anyview/StudentServlet?action=ifHaveChoose",
                    data:{"cid":cid,"sid":sid},
                    success:function (data) {
                        if(data.result1){
                            alert("已选不可重复！")
                        }
                        if(data.result2){
                            alert("选课成功！");
                            window.location.href="http://localhost:8080/Anyview/StudentServlet?action=queryAll";
                        }
                        if(data.result3){
                            alert("未到选课时间！");
                        }
                        if(data.result4){
                            alert("人数已满！");
                        }
                    },
                    dataType:"json"
                })


            })

        })
*/
        /* $(function () {
            $("#delete").click(function () {
                var cid=document.getElementById("delete").value;
                var sid=
                var para= {

                    action:'queryAll',




                };
                $.ajax({
                    type:"POST",
                    url:"http://localhost:8080/Anyview/StudentServlet?action=deleteCource",
                    data:{"cid":cid,"sid":sid},
                    success:function (data) {
                        if(data.result1){
                            alert("还未选课！")
                        }
                        if(data.result2){
                            alert("退选成功！");
                            window.location.href="http://localhost:8080/Anyview/StudentServlet?"+$.param(para);
                        }

                    },
                    dataType:"json"
                })


            })

        })

*/

    </script>                                                                                                                                   </script>                                                                                                                                        </script>
</head>
<body>
<div><a href="StudentServlet?action=queryMyCourses&sid=${sessionScope.student.id}">我的课表</a></div>
<h1 align="center">学生信息页面</h1>
<div align="center">
    <table>
    <tr>
        <th>序号</th>
        <th>课程名字</th>
        <th>上课地点</th>
        <th>任课老师2</th>
        <th>上课时间</th>
        <th>最大人数</th>
        <th>开始选课时间</th>
        <th>已选人数</th>
    </tr>
<c:forEach items="${requestScope.courses}" var="Course" varStatus="status"><br>
        <tr>
            <td>${ status.index + 1}</td>
            <td>${Course.name}</td>
            <td>${Course.place}</td>
            <td>${Course.teacher}</td>
            <td>${Course.TIME}</td>
            <td>${Course.total}</td>
            <td>${Course.beginTime}</td>
            <td>${Course.number}</td>
            <td><button id="insert" value="${Course.id}">选课</button> </td>
            <td><button id="delete" value="${Course.id}">退选</button> </td>
        </tr>
</c:forEach>
    </table>
</div >
</body>
</html>
